package com.zhongxiangspace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.StringUtil;
import com.google.gson.Gson;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.entity.VO.QueryAnalyzerOffice;
import com.zhongxiangspace.enums.AreaExtentEnum;
import com.zhongxiangspace.enums.DecorationEnum;
import com.zhongxiangspace.enums.PagesEnum;
import com.zhongxiangspace.enums.PriceExtenEnum;
import com.zhongxiangspace.result.PageResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.AdvertService;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.HouseService;
import com.zhongxiangspace.tools.AnalyzerConditionTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SuperiorHouseController {

    /**
     * 房源
     */
    @Autowired
    private HouseService houseService;
    /**
     * 区域
     */
    @Autowired
    private CountyService countyService;

    /**
     * 精选房源
     *
     * @param value
     * @param model
     * @return
     */
    @GetMapping("lists/sh{value}")
    public String superiorHouse(@PathVariable("value") String value, Model model) {
        Map<String, Object> data = new HashMap<>();
        QueryAnalyzerOffice queryAnalyzerOffice = new QueryAnalyzerOffice();
        //获取所有主城主区域
        List<County> countyList = this.countyService.queryAllType(null);
        model.addAttribute("countyList", countyList);

        //根据区域id 获取所有商圈
        Integer region_id = AnalyzerConditionTools.substringId(value, "r");
        if (region_id != null) {
            queryAnalyzerOffice.setCounty_id(region_id);
            data.put("county", AnalyzerConditionTools.getCondition(value, "r"));
        }
        //根据获取商圈id
        Integer tradeAreaId = AnalyzerConditionTools.substringId(value, "t");
        if (tradeAreaId != null) {
            queryAnalyzerOffice.setTradeArea_id(tradeAreaId);
            data.put("tradeArea", AnalyzerConditionTools.getCondition(value, "t"));
        }
        //面积
        String area = AnalyzerConditionTools.getCondition(value, "a");//缓存面积id
        if (StringUtil.isNotEmpty(area)) {
            queryAnalyzerOffice.setMinArea(AreaExtentEnum.getEnumObject(area).getMin());
            queryAnalyzerOffice.setMaxArea(AreaExtentEnum.getEnumObject(area).getMax());
            data.put("area", area);
        }
        //单价
        String price = AnalyzerConditionTools.getCondition(value, "p");//缓存金额
        if (StringUtil.isNotEmpty(price)) {
            queryAnalyzerOffice.setMinPrice(PriceExtenEnum.getEnumObject(price).getMin());
            queryAnalyzerOffice.setMaxPrice(PriceExtenEnum.getEnumObject(price).getMax());
            data.put("price", price);
        }
        //装修
        String decorate = AnalyzerConditionTools.getCondition(value, "d");//缓存装修
        if (StringUtil.isNotEmpty(decorate)) {
            queryAnalyzerOffice.setDecorate(String.valueOf(DecorationEnum.getEnumObject(decorate).getValue()));
            data.put("decorate", decorate);
        }
        model.addAttribute("data", data);
        String gson = new Gson().toJson(queryAnalyzerOffice);
        model.addAttribute("queryAnalyzerOffice", gson);
        return "list/superiorHouse_list_mobile";
    }

    /**
     * 异步加载精选房源
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/lists/ajaxFineHouses")
    public Result ajaxFineHouses(QueryAnalyzerOffice queryAnalyzerOffice) {
        PageHelper.startPage(queryAnalyzerOffice.getPage().intValue(), 8, "house.topCode DESC");
        Page<Map<String, Object>> listHouse = (Page<Map<String, Object>>) this.houseService.queryAllhouse(queryAnalyzerOffice);
        PageResult pageNotHouseList = new PageResult(listHouse.getPageNum(), listHouse.getPageSize(), listHouse.getTotal(), listHouse.getPages(), listHouse.getResult(), true);
        return pageNotHouseList;
    }

    ;


}
